﻿using SqlPerfomance.V2.DAL;
using SqlPerfomance.V2.DomainModel;
using SqlPerfomance.V2.DomainModel.Enums;
using SqlPerfomance.V2.DomainModel.Models;
using SqlPerfomance.V2.DomainModel.NodeTree.Interfaces;
using SqlPerfomance.V2.Tests.Code;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SqlPerfomance.V2.Tests.TestsCommand
{
    public abstract class BaseTestCommand<T> : ITestCommand
    {
        protected string _tableName = string.Empty;
        protected ConnectionStrings _connectionString = ConnectionStrings.SqlServerConnectionString;
        public BaseTestCommand(ConnectionStrings connectionString)
        {
            _connectionString = connectionString;
            _tableName = TableNameFactory.GetTableName(EnumHierarchyTestType);
        }

        protected abstract RealDataRepository<T> Repository { get; }

        protected abstract ISqlTestOperation Tests { get; }

        protected abstract EnumHierarchyTestType EnumHierarchyTestType { get; } 

        public TestCommandResult RunTests(TestSettings settings, INode<CommonTreeNode> commonTree)
        {
            if (settings == null)
                throw new ArgumentNullException("settings");
            Repository.ClearTable(_connectionString, _tableName);
            Repository.ClearCache(_connectionString);
            var tree = Repository.CreateTree((INode<CommonTreeNode>)commonTree);
            Repository.SaveRange(tree.Nodes, _connectionString);
            
            var testResults = new List<TestResult>();

            testResults.Add(Tests.TestSelectAll());
            testResults.Add(Tests.TestSelectBranchById(2));
            testResults.Add(Tests.TestSelectChildrenById());
            testResults.Add(Tests.TestSelectParentById());
            testResults.Add(Tests.TestSelectPathById());
            testResults.Add(Tests.TestAddNode());
            testResults.Add(Tests.TestMoveNode());
            testResults.Add(Tests.TestDeleteNode());

            return new TestCommandResult
            {
                HierarchyTestType = EnumHierarchyTestType,
                TestsResults = testResults,
                Settings = settings
            };
        }
    }
}
